import Vue from 'vue'
import VueRouter from 'vue-router'
import layout from '../views/layout'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/layout',
  },
  {
    path: '/layout',
    name: 'layout',
    component: layout,
    meta: {
      needLogin: true,
    },
    children: [
      {
        path:'/layout',
        redirect: '/layout/home'
      },
      {
        path:'/layout/home',
        component:  () => import(/* webpackChunkName: "about" */ '../views/layout/home'),
        children: [
          {
            path: '/layout/home',
            redirect: '/layout/home/homeList1'
          },
          {
            path: 'homeList1',
            component:  () => import(/* webpackChunkName: "about" */ '../views/layout/home/pages/homeList1'),
          },
          {
            path: 'homeList2',
            component:  () => import(/* webpackChunkName: "about" */ '../views/layout/home/pages/homeList2'),
          }
        ]
      },
      {
        path:'/layout/list',
        component:  () => import(/* webpackChunkName: "about" */ '../views/layout/list'),
      },
      {
        path:'/layout/cart',
        component:  () => import(/* webpackChunkName: "about" */ '../views/layout/cart'),
      },{
        path:'/layout/my',
        component:  () => import(/* webpackChunkName: "about" */ '../views/layout/my'),
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "about" */ '../views/login'),
  },
  {
    path: '/detail/:id',
    name: 'detail',
    component: () => import(/* webpackChunkName: "about" */ '../views/detail'),
  },
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  if (to.meta.needLogin) {
    if (window.sessionStorage.getItem('token')) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})
export default router
